﻿using Google.Apis.Services;
using Google.Apis.YouTube.v3;
using Google.Apis.YouTube.v3.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Playlist.Controllers
{
    public class RequestController : Controller
    {
        public ActionResult Index(string q)
        {
            YouTubeController objServicio = new YouTubeController();
            var resultado = objServicio.Get();

            ViewData["lstVideos"] = getVideos(q, 11);
            return View();
        }

        public List<SearchResult> getVideos(string q, int maxResults)
        {
            List<SearchResult> lstResultados = new List<SearchResult>();
            List<string> videos = new List<string>();

            var youtubeServiceVideos = new YouTubeService(new BaseClientService.Initializer()
            {
                ApplicationName = "Playlist",
                ApiKey = "AIzaSyBAlePROILbw46BkzsBSR2UZFWqTxUoVAE",
            });

            var searchListRequest = youtubeServiceVideos.Search.List("snippet");
            searchListRequest.Q = q;
            searchListRequest.MaxResults = maxResults;

            var searchListResponse = searchListRequest.Execute();

            foreach (var searchResult in searchListResponse.Items)
            {
                switch (searchResult.Id.Kind)
                {
                    case "youtube#video":
                        videos.Add("https://www.youtube.com/embed/" + searchResult.Id.VideoId);
                        lstResultados.Add(searchResult);
                        break;

                    default:
                        break;
                }
            }

            return lstResultados;
        }
    }
}